package com.likk.usermanage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.likk.usermanage.pojo.User;
import com.likk.usermanage.service.UserService;

@Controller
@RequestMapping("rest/user")
public class RestFulUserController {

	@Autowired
	private UserService userService;
	
	/**
	 * 根据id查询用户信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="{id}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<User> queryUserById(@PathVariable("id") Long id){
		User user;
		try {
			if(id.longValue() < 1){
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
			}
			user = this.userService.queryUserById(id);
			if(null == user){
				// 资源不存在，状态码响应：404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			// 200
//		return ResponseEntity.status(HttpStatus.OK).body(user);
			return ResponseEntity.ok(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 500
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	/**
	 * 新增用户数据
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<Void> saveUser(User user) {
		try {
			if(user.getUserName() == null || "".equals(user.getUserName())){
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			this.userService.addUser(user);
			// 201
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// 500
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	@RequestMapping(method = RequestMethod.PUT)
	public ResponseEntity<Void> updateUser(User user){
		
		try {
			if(user.getId() == null || user.getId().longValue() < 1){
				// 参数有误
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			this.userService.updateUser(user);
			// 204
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 500
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	@RequestMapping(method = RequestMethod.DELETE)
	public ResponseEntity<Void> deleteUserById(@RequestParam(value = "id", defaultValue="0")Long id){
		
		try {
			if(id.intValue() == 0){
				// 参数有误,响应400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			this.userService.deleteUserById(id);
			// 204
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 500
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
}












